import ClientSubmitInfo from './ClientSubmitInfo';
import { IStreamMode } from './interface';

interface IClient {
	id: number | string;
	label: string;
	audioOnly: boolean;
	clientId: string;
	videoId: string;
	muteStatus: boolean;
	streamMode: IStreamMode;
	role: 'customer';
	isScreenSyncMode: boolean;
	userName: string;
}

class Client extends ClientSubmitInfo {
	private clientMembers: Map<string, IClient>;
	private static instance: Client | null;
	static getInstance(): Client {
		// 判断系统是否已经有这个单例
		if (Client.instance === null) {
			Client.instance = new Client();
		}
		return Client.instance; // 单例模式
	}
	constructor() {
		super();
		this.clientMembers = new Map();
	}

	public addClient = (clientId: string, client: IClient) => {
		this.clientMembers.set(clientId, client);
		this.addClientInfo(clientId, {});
	};

	public removeClient = (clientId: string) => {
		if (this.hasInMembers(clientId)) {
			this.clientMembers.delete(clientId);
		}
	};

	public clear = () => {
		this.clientMembers.clear();
		this.clearClientSubmitInfo();
	};

	public getClientName = (clientId: string) => {
		const client = this.getClientInMemebrs(clientId);
		return client?.userName;
	};

	private hasInMembers = (clientId: string) => {
		return this.clientMembers.has(clientId);
	};

	private getClientInMemebrs = (clientId: string) => {
		if (this.hasInMembers(clientId)) {
			return this.clientMembers.get(clientId);
		}
		return null;
	};

	/** 更新访客属性状态 */
	public setClientStream = (clientId: string, mode: IStreamMode) => {
		const client = this.getClientInMemebrs(clientId);
		client && this.clientMembers.set(clientId, { ...client, streamMode: mode });
	};

	public setClientScreenSyncMode = (clientId: string, isScreenSyncMode: boolean) => {
		const client = this.getClientInMemebrs(clientId);
		client && this.clientMembers.set(clientId, { ...client, isScreenSyncMode: isScreenSyncMode });
	};

	public setMuteStatus = (clientId: string, muteStatus: boolean) => {
		const client = this.getClientInMemebrs(clientId);
		client && this.clientMembers.set(clientId, { ...client, muteStatus: muteStatus });
	};

	public setAudioStatus = (clientId: string, audioOnly: boolean) => {
		const client = this.getClientInMemebrs(clientId);
		client && this.clientMembers.set(clientId, { ...client, audioOnly: audioOnly });
	};

	/** 访客执行业务指令 */
	public receiveBusiness = () => {};
}

export default new Client();
